<?php
if (!defined('IN_IA')) {
    exit('Access Denied');
}
global $_W, $_GPC;
$commission_set = globalSetting('commission');
$op     = $operation = $_GPC['op'] ? $_GPC['op'] : 'display';
$groups = m('member')->getGroups();
$levels = m('member')->getLevels();
$shop   = globalSetting('shop');
if ($op == 'display') {
    $pindex    = max(1, intval($_GPC['page']));
    $psize     = 20;
    $condition = " and dm.uniacid=:uniacid";
    $params    = array(
        ':uniacid' => $_W['uniacid']
    );
    if (!empty($_GPC['realname'])) {
        $_GPC['realname'] = trim($_GPC['realname']);
        $condition .= ' and ( dm.realname like :realname or dm.nickname like :realname or dm.mobile like :realname or dm.openid=:ropenid)';
        $params[':realname'] = "%{$_GPC['realname']}%";
         $params[':ropenid'] = "{$_GPC['realname']}";
    }

    if ($_GPC['groupid'] != '') {
        $condition .= ' and dm.groupid=' . intval($_GPC['groupid']);
    }
 
    if ($_GPC['isblack'] != '') {
        $condition .= ' and dm.isblack=' . intval($_GPC['isblack']);
    }
    $sql = "select dm.id,dm.openid,dm.avatar,dm.nickname,q.new_up_num,q.timestamp from " . tablename('eshop_member') . " dm " . " left join " . tablename('eshop_member_group') . " g on dm.groupid=g.id" . " left join " . tablename('eshop_member_message_queue') . " q on q.uid=dm.id" . " where q.uid is not null {$condition}  ORDER BY q.timestamp DESC";
    if (empty($_GPC['export'])) {
        $sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;
    }
    $list = pdo_fetchall($sql, $params);
    foreach ($list as &$row) {
        $row['levelname']  = empty($row['levelname']) ? (empty($shop['levelname']) ? '普通会员' : $shop['levelname']) : $row['levelname'];
    }
    unset($row);
    $total           = pdo_fetchcolumn("select count(*) from" . tablename('eshop_member') . " dm " . " left join " . tablename('eshop_member_group') . " g on dm.groupid=g.id" . " left join " . tablename('eshop_member_message_queue') . " q on q.uid=dm.id" . " where q.uid is not null {$condition} ", $params);
    $pager           = pagination($total, $pindex, $psize);
} else if ($op == 'detail') {
    if (checksubmit('submit')){
        $params    = array(
            'id' => 0,
            'uid' => $_GPC['id'],
            'type' => 1,
            'content' => $_GPC['data']['content']
        );
        pdo_insert('eshop_member_message',$params);
        
        $condition = " and uid=" . intval($_GPC['id']);
        $sql = "select * from " . tablename('eshop_member_message_queue') . " where 1 {$condition}";
        $list = pdo_fetchall($sql);
        if(empty($list)){
            $params    = array(
                'uid' => $_GPC['id'],
                'new_up_num' => 0,
                'new_down_num' => 1
            );
            pdo_insert('eshop_member_message_queue',$params);
        }
        else{
            $sql = "update " . tablename('eshop_member_message_queue') . " set new_down_num = new_down_num + 1 where 1 {$condition}";
            pdo_query($sql);
            
        }
        message("回复成功！", $this->createWebUrl('member/message'), 'success'); 
    }
    else{
        $condition = " and uid=" . intval($_GPC['id']);
        $sql = "update " . tablename('eshop_member_message_queue') . " set new_up_num = 0 where 1 {$condition}";
        pdo_query($sql);

        $condition = " and id=" . intval($_GPC['id']);
        $sql = "select * from " . tablename('eshop_member') . " where 1 {$condition}";
        $member = pdo_fetchall($sql)[0];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        $condition = " and uid=" . intval($_GPC['id']);
        $sql = "select * from " . tablename('eshop_member_message') . " where 1 {$condition} ORDER BY timestamp DESC";
        $sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;
        $list = pdo_fetchall($sql);
        $total           = pdo_fetchcolumn("select count(*) from " . tablename('eshop_member_message') . " where 1 {$condition}", $params);
        $pager           = pagination($total, $pindex, $psize);
    }
    
}

include $this->template('message');